package com.jtpay.payresulthelper.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import com.jtpay.payresulthelper.util.LogUtil;

public class AliPayDataHelper extends SQLiteOpenHelper {
    private static final String SQL_CREATE_TABLE = "CREATE TABLE ";
    private static final String SQL_AUTOINCREMENT_KEY = " INTEGER PRIMARY KEY AUTOINCREMENT";
    private static final String SQL_PRIMARY_KEY_DEFAULT_0 = " INTEGER DEFAULT 0";
    private static final String SQL_TEXT = " TEXT";
    private static final String SQL_TEXT_PRIMARY_KEY = " TEXT UNIQUE";
    private static final String SQL_COMMA = ",";
    private static final String DATABASE_NAME = "alipay_product.db";
    private static final int VERSION = 4;
    private static final String TEMP = "_temp_";
    private String CREATE_TABLE_ALI_PAY = SQL_CREATE_TABLE + AliPayColumn._TABLE_NAME
            + " (" + AliPayColumn._ID + SQL_AUTOINCREMENT_KEY + SQL_COMMA
            + AliPayColumn._MONEY + SQL_TEXT + SQL_COMMA
            + AliPayColumn._REMARK + SQL_TEXT + SQL_COMMA
            + AliPayColumn._IS_OK + SQL_PRIMARY_KEY_DEFAULT_0 + SQL_COMMA
            + AliPayColumn._ORDER_NUMBER + SQL_TEXT_PRIMARY_KEY + SQL_COMMA
            + AliPayColumn._DATE + SQL_TEXT + SQL_COMMA
            + AliPayColumn._TIME_STAMP + SQL_TEXT + SQL_COMMA
            + AliPayColumn._ACCOUNT + SQL_TEXT
            + ");";

    private String CREATE_TABLE_ALI_ORDER_TASK = SQL_CREATE_TABLE + AliQueryOrderColumn._TABLE_NAME
            + " (" + AliQueryOrderColumn._ID + SQL_AUTOINCREMENT_KEY + SQL_COMMA
            + AliQueryOrderColumn._REMARK + SQL_TEXT + SQL_COMMA
            + AliQueryOrderColumn._IS_OK + SQL_PRIMARY_KEY_DEFAULT_0 + SQL_COMMA
            + AliQueryOrderColumn._ORDER_NUMBER + SQL_TEXT + SQL_COMMA
            + AliQueryOrderColumn._TIME_STAMP_BEGIN + SQL_TEXT_PRIMARY_KEY + SQL_COMMA
            + AliQueryOrderColumn._TIME_STAMP_END + SQL_TEXT_PRIMARY_KEY
            + ");";

    private String CREATE_TABLE_TEMP_ALI_PAY = SQL_CREATE_TABLE + AliPayColumn._TABLE_TEMP_NAME
            + " (" + AliPayColumn._ID + SQL_AUTOINCREMENT_KEY + SQL_COMMA
            + AliPayColumn._MONEY + SQL_TEXT + SQL_COMMA
            + AliPayColumn._REMARK + SQL_TEXT + SQL_COMMA
            + AliPayColumn._IS_OK + SQL_PRIMARY_KEY_DEFAULT_0 + SQL_COMMA
            + AliPayColumn._ORDER_NUMBER + SQL_TEXT_PRIMARY_KEY + SQL_COMMA
            + AliPayColumn._DATE + SQL_TEXT + SQL_COMMA
            + AliPayColumn._TIME_STAMP + SQL_TEXT + SQL_COMMA
            + AliPayColumn._ACCOUNT + SQL_TEXT
            + ");";

    private String CREATE_TABLE_ALI_PAY_TEMP = "alter table " + AliPayColumn._TABLE_NAME + " rename to " + TEMP + AliPayColumn._TABLE_NAME;
    private String INSERT_DATA = "insert into " + AliPayColumn._TABLE_NAME + " select *,'' from " + TEMP + AliPayColumn._TABLE_NAME;
    private String DROP_ALI_PAY_TEMP = "drop table " + TEMP + AliPayColumn._TABLE_NAME;
    private String ADD_ALI_INDEX = "CREATE INDEX indexVersion" + VERSION + " ON " + AliPayColumn._TABLE_NAME
            + "(" + AliPayColumn._IS_OK + SQL_COMMA + AliPayColumn._DATE +
            SQL_COMMA + AliPayColumn._ACCOUNT + ")";

    public AliPayDataHelper(Context context) {
        super(context, DATABASE_NAME, null, VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TABLE_ALI_PAY);
        db.execSQL(ADD_ALI_INDEX);
        db.execSQL(CREATE_TABLE_TEMP_ALI_PAY);
        db.execSQL(CREATE_TABLE_ALI_ORDER_TASK);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        LogUtil.log("onUpgrade oldVersion " + oldVersion + " newVersion " + newVersion);
        if (oldVersion == 2 && newVersion == VERSION) {
            db.execSQL(ADD_ALI_INDEX);
        } else if (oldVersion == 1 && newVersion == VERSION) {
            db.execSQL(CREATE_TABLE_ALI_PAY_TEMP);
            db.execSQL(CREATE_TABLE_ALI_PAY);
            db.execSQL(INSERT_DATA);
            db.execSQL(DROP_ALI_PAY_TEMP);
            db.execSQL(ADD_ALI_INDEX);
        } else if (oldVersion == 3 && newVersion == VERSION) {
            db.execSQL(CREATE_TABLE_TEMP_ALI_PAY);
            db.execSQL(CREATE_TABLE_ALI_ORDER_TASK);
        }
    }
}
